﻿#region Copyright
//Copyright 2012 Phelon Aka. Jon H.
/*
    This file is part of BGBuddy - Ultimate PVP Suite.

    BGBuddy - Ultimate PVP Suite is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BGBuddy - Ultimate PVP Suite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Styx.Plugins;
using Styx;
using Styx.Helpers;
using Styx.CommonBot.POI;
using Styx.Pathing;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Tripper.Tools.Math;

namespace UltimatePvPSuite
{
    public static class Movement
    {
        private static Random strafeTimer = new Random();

        public static bool MoveTo(WoWUnit Unit)
        {
            //StopMovement();
            if (UltimatePvPSuite.DisabledCCMovement &&
                Navigator.CanNavigateFully(StyxWoW.Me.Location, StyxWoW.Me.CurrentTarget.Location))
            {
                if (StyxWoW.Me.CurrentTarget.Distance < 10)
                {
                    if (!StyxWoW.Me.MovementInfo.MovingForward)
                    {
                        StopMovement(true, true, true, true);
                        WoWMovement.Move(WoWMovement.MovementDirection.Forward);
                    }
                }
                else
                {
                    Navigator.MoveTo(StyxWoW.Me.CurrentTarget.Location);
                }
                return false;
            }
            return true;
        }

        public static void StopMovement(bool forward, bool backward, bool left, bool right)
        {
            if (StyxWoW.Me.MovementInfo.MovingStrafeRight)
            {
                WoWMovement.MoveStop(WoWMovement.MovementDirection.StrafeRight);
                //Styx.Helpers.KeyboardManager.KeyUpDown((char)Keys.E);
            }

            if (StyxWoW.Me.MovementInfo.MovingStrafeLeft)
            {
                WoWMovement.MoveStop(WoWMovement.MovementDirection.StrafeLeft);
                //Styx.Helpers.KeyboardManager.KeyUpDown((char)Keys.Q);
            }

            if (StyxWoW.Me.MovementInfo.MovingForward)
            {
                WoWMovement.MoveStop(WoWMovement.MovementDirection.Forward);
                //Styx.Helpers.KeyboardManager.KeyUpDown((char)Keys.W);
            }

            if (StyxWoW.Me.MovementInfo.MovingBackward)
            {
                WoWMovement.MoveStop(WoWMovement.MovementDirection.Backwards);
                //Styx.Helpers.KeyboardManager.KeyUpDown((char)Keys.D);
            }
        }

        public static void Strafe()
        {
            Random randemote = new Random();
            if (StyxWoW.Me.CurrentTarget.Distance <= Convert.ToInt32(UltimatePvPSuite.StrafeDistance))
            {
                if (!StyxWoW.Me.MovementInfo.MovingStrafeRight && !StyxWoW.Me.MovementInfo.MovingStrafeLeft)
                {
                    if (StyxWoW.Me.BehindTarget && UltimatePvPSuite.StayBehindTarget)
                    { }
                    else
                    {
                        Logger.Write("Strafe Start.");
                        Thread.Sleep(250);
                        WoWMovement.MovementDirection way = (Environment.TickCount & 1) == 0 ? WoWMovement.MovementDirection.StrafeLeft : WoWMovement.MovementDirection.StrafeRight;
                        WoWMovement.Move(way, TimeSpan.FromSeconds(strafeTimer.Next(5500, 9500)));
                    }
                }
            }
        }
        /// <summary>
        /// Calculate the Point behind the Target
        /// </summary>
        /// <returns></returns>
        public static WoWPoint CalculatePointBehindTarget()
        {
            return
                StyxWoW.Me.CurrentTarget.Location.RayCast(
                    StyxWoW.Me.CurrentTarget.Rotation + WoWMathHelper.DegreesToRadians(150), 3.2f);
        }
        /// <summary>
        /// Navigation Check from Point to Point
        /// </summary>
        /// <param name="Location1"></param>
        /// <param name="Location2"></param>
        /// <returns></returns>
        public static WoWPoint NavMe(WoWPoint Location1, WoWPoint Location2)
        {
            if (StyxWoW.Me.CurrentTarget != null)
            {
                double Distance = StyxWoW.Me.CurrentTarget.Distance + 10;
                return WoWMathHelper.CalculatePointFrom(StyxWoW.Me.Location, StyxWoW.Me.CurrentTarget.Location, (float)Distance);
            }
            return StyxWoW.Me.Location;
        }
    }
}
